package peppa.george.book.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import peppa.george.base.Util.JsonResult;
import peppa.george.base.Util.PageUtil;
import peppa.george.book.domain.Book;
import peppa.george.book.query.BookQuery;
import peppa.george.book.service.IBookService;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private IBookService bookService;

    // 查询全部
    @GetMapping
    public List<Book> findAll(){
        return bookService.findAll();
    }
    // 查询单条
    @GetMapping("/{id}")
    public Book findById(@PathVariable("id")Long id){
        return bookService.findById(id);
    }
    // 删除
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")Long id){
        bookService.delete(id);
        return JsonResult.me().setMsg("请求成功");
    }
    // 修改
    @PutMapping
    public JsonResult saveOrUpdate(@RequestBody Book book){
        if (book.getId() == null){
            bookService.save(book);
        } else {
            bookService.update(book);
        }
        return JsonResult.me().setMsg("请求成功");
    }
    @PostMapping
    public PageUtil<Book> queryByPage(@RequestBody BookQuery bookQuery){
        return bookService.queryByPage(bookQuery);
    }
}
